﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDTB.Common;
using LDTB.Common.Entities;

namespace LDTB.UI
{
    public partial class EditWard : UIBase
    {

        int id = -1;
        public int IDWard
        {
            get
            {
                if (Request["ID"] != null)
                {
                    if (!int.TryParse(Request["ID"].ToString(), out id))
                    {
                        id = -1;
                    }
                }
                return id;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (IDWard != -1)
                {
                    LoadWard();
                    this.Button1.Text = "Lưu lại";
                }
                else
                {
                    this.Button1.Text = "Thêm mới";
                }
            }
        }

        private void LoadWard()
        {
            var ward = (from w in Data.VC_Huyens
                        where w.ID == IDWard
                        select w).FirstOrDefault();

            if (ward != null)
            {
                txt_Ward.Text = ward.Name;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (IDWard == -1)
            {
                var w = new VC_Huyen { Name = txt_Ward.Text.Trim() };
                Data.VC_Huyens.InsertOnSubmit(w);
            }
            else
            {

                var ward = (from w in Data.VC_Huyens
                            where w.ID == IDWard
                            select w).FirstOrDefault();

                ward.Name = txt_Ward.Text.Trim();

            }
            Data.SubmitChanges();
            Response.Redirect(EditUrl("Ward"));
        }
    }
}